package com.abl.common.mybatis.strengthen;

import com.abl.common.mybatis.model.Dnt;

import java.util.List;

//填充
public class DbFillImpl implements DbFill {

    @Override
    public void insertFill(Dnt dnt) {
        fill(dnt, FillColumn.insert.keys());
    }

    @Override
    public void updateFill(Dnt dnt) {
        fill(dnt, FillColumn.update.keys());
    }

    @Override
    public void deleteFill(Dnt dnt) {
        dnt.set(FillColumn.d_s.key(), "1");
        fill(dnt, FillColumn.delete.keys());
    }

    @Override
    public void wrapperDeleteFill(Wrapper<Object> wrapper) {
        wrapper.set(FillColumn.d_s.key(), "1");
        fill(wrapper, FillColumn.delete.keys());
    }

    @Override
    public void wrapperUpdateFill(Wrapper<Object> wrapper) {
        fill(wrapper, FillColumn.update.keys());
    }


    @Override
    public void insertFill(Dnt dnt, FillColumn... fillColumns) {
        for (FillColumn fillColumn : fillColumns) {
            fill(dnt, fillColumn.key());
        }

    }

    @Override
    public void updateFill(Dnt dnt, FillColumn... fillColumns) {
        for (FillColumn fillColumn : fillColumns) {
            fill(dnt, fillColumn.key());
        }
    }

    @Override
    public void deleteFill(Dnt dnt, FillColumn... fillColumns) {
        for (FillColumn fillColumn : fillColumns) {
            fill(dnt, fillColumn.key());
        }
    }

    @Override
    public void wrapperDeleteFill(Wrapper<Object> wrapper, FillColumn... fillColumns) {
        for (FillColumn fillColumn : fillColumns) {
            fill(wrapper, fillColumn.key());
        }
    }

    @Override
    public void wrapperUpdateFill(Wrapper<Object> wrapper, FillColumn... fillColumns) {
        for (FillColumn fillColumn : fillColumns) {
            fill(wrapper, fillColumn.key());
        }
    }

    private void fill(Dnt dnt, String key) {
        FillColumn fillColumn = FillColumn.valueOf(key);
        dnt.set(key, fillColumn.defaultValue());
    }

    private void fill(Dnt dnt, List<String> keys) {
        for (String key : keys) {
            fill(dnt, key);
        }
    }

    private void fill(Wrapper<Object> wrapper, List<String> keys) {
        for (String key : keys) {
            fill(wrapper, key);
        }
    }

    private void fill(Wrapper<Object> wrapper, String key) {
        FillColumn fillColumn = FillColumn.valueOf(key);
        wrapper.set(key, fillColumn.defaultValue());
    }

}